package com.ebelter.bodyfatscale.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.moudules.https.interfaces.HttpResponse;
import com.ebelter.bodyfatscale.moudules.https.response.HttpsParameters;
import com.ebelter.bodyfatscale.moudules.https.response.OriginalParametersResponse;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.util.ULog;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

public class ModifyActivity extends BaseActivity {

    public static final String DISPLAY_VALUE = "DISPLAY_VALUE";
    public static final String MODIFY_STYLE = "MODIFY_STYLE";
    public static final int MODIFY_NAME = 1;
    public static final int MODIFY_EMAIL = 2;
    private static final String TAG = "ModifyActivity";
    public int CURRENT_MODIFY_STYLE = 0;

    private String displayValue;

    @BindView(R.id.modify_ed)
    EditText modify_ed;

    private NetUtils myNetUtils;
    @Override
    protected int loadLayoutById() {
        return R.layout.activity_modify;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        CURRENT_MODIFY_STYLE = getIntent().getIntExtra(MODIFY_STYLE,0);
        displayValue = getIntent().getStringExtra(DISPLAY_VALUE);
        if (CURRENT_MODIFY_STYLE==0){
            finish();
        }else if (CURRENT_MODIFY_STYLE==MODIFY_NAME){

            modify_ed.setHint(R.string.nike_name);
            modify_ed.setMaxEms(20);

        }else if (CURRENT_MODIFY_STYLE==MODIFY_EMAIL){
            modify_ed.setHint(R.string.email);

        }

        myNetUtils = NetUtils.getInstance();

        modify_ed.setText(displayValue);

//        modify_ed.addTextChangedListener(new MyTextWatcher() {
//            @Override
//            public void afterTextChanged(Editable s) {
//                final String afterStr = s.toString();
//                if (TextUtils.isEmpty(afterStr)||TextUtils.equals(afterStr,displayValue)){
//                    return;
//                }
//
//                boolean isEmail=CURRENT_MODIFY_STYLE==MODIFY_EMAIL;
//
//                myNetUtils.validateEmailorNicknameRequest(ModifyActivity.this, afterStr, isEmail, new HttpResponse<ValidateEmailorNicknameResponse>() {
//                    @Override
//                    public void result(boolean isRequestSuccess, String requestDes, ValidateEmailorNicknameResponse validateEmailorNicknameResponse) {
//                        if (isRequestSuccess){
//                            if (validateEmailorNicknameResponse.resultCode==0){
//                                //ToastUtil.show(afterStr+" " + StringUtils.getResStr(R.string.can_be_used), Gravity.CENTER);
//                            }else {
//                                ToastUtil.show(afterStr +" " +StringUtils.getResStr(R.string.have_been_registered), Gravity.CENTER);
//                                ViewUtils.shakeView(modify_ed);
//                            }
//                        }
//                    }
//                });
//
//            }
//        });

    }


    @OnClick({ R.id.modify_back_bt,R.id.modify_done_tv})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modify_back_bt:
                finish();
                break;
            case R.id.modify_done_tv:
                //ToastUtil.show(modify_ed.getText().toString());

                String modifyStr=modify_ed.getText().toString().trim();

                if (modifyStr.length()>20){
                    ToastUtil.show(R.string.nikename_over_tip);
                    return;
                }

                final Map<String,Serializable> paramMap=new HashMap<>();
                paramMap.put(HttpsParameters.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
                if (CURRENT_MODIFY_STYLE==MODIFY_EMAIL){
                    throw new RuntimeException("没有修改邮箱这一项了");
                }else if (CURRENT_MODIFY_STYLE==MODIFY_NAME){
                    paramMap.put(HttpsParameters.NICK_NAME,modifyStr);
                }

                if (!NetUtils.available()){
                    ToastUtil.show(R.string.Network_error);
                    return;
                }

                myNetUtils.updateInfo(ModifyActivity.this, paramMap, new HttpResponse<OriginalParametersResponse>() {
                    @Override
                    public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse) {
                        ULog.i(TAG,"当前线程："+Thread.currentThread().getName());
                        if (isRequestSuccess){

                            if (originalParametersResponse.resultCode==0){
                                ToastUtil.show(R.string.Modify_Success);
                                updateLocalData((Map<String, Serializable>) originalParametersResponse.originalParameters);
                                finish();

                            }else if (originalParametersResponse.resultCode==112){
                                ToastUtil.show(R.string.nickname_have_been_registered);
                            }else if (originalParametersResponse.resultCode==110){
                                ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                            }else {
                                ToastUtil.show(R.string.Modify_Failed);
                            }

                        }else {
                            ToastUtil.show(R.string.Network_connection_error);
                        }
                    }
                });
                break;


        }
    }

    //更新本地数据
    private void updateLocalData(Map<String, Serializable> originalParameters) {
        if (originalParameters==null){
            return;
        }
        Set<String> sets = originalParameters.keySet();
        for (String k:sets){
            if (TextUtils.isEmpty(k)){
                continue;
            }
            Serializable serializable = originalParameters.get(k);
            if (TextUtils.equals(HttpsParameters.NICK_NAME,k)){
                //说明更改了昵称
                UserSpUtil.writeString(Constants.IUser.NICK_NAME, (String) serializable);
                displayValue=(String) serializable;
            }
        }
    }
}
